package main;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import entity.Pubmed;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import utils.PubmedUtils2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022-3-7 15:30
 */
public class XML {

    static String insetTable = "pubmed_vip_extend_new_new";


    @SneakyThrows
    public static void main(String[] args) {

        Db use = Db.use(DSFactory.get("server_local"));

////        String compare = "select * from compare_doi where pmid >33555351 and pmid <33558662 ";
//        String compare = "select * from compare_doi where pmid =34242582";
//        List<Entity> query = use.query(compare);
//        for (Entity entity : query) {
//            String pmid = entity.getStr("pmid");
//            Entity entityS = use.queryOne("select * from xml_content_3qw where pmid = " + pmid);
//            String content = ZipUtil.unGzip(entityS.getBytes("content"), "utf-8");
//            FileUtil.writeString(content, "/usr/local/" + pmid + ".xml", "UTF-8");
//        }


        String pmid = "25927118";
        Entity entity1 = use.queryOne("select * from xml_content_1_3qw where pmid = " + pmid);
        if (entity1 != null) {
            String content = ZipUtil.unGzip(entity1.getBytes("content"), "utf-8");
            FileUtil.writeString(content, "/usr/local/" + pmid + ".xml", "UTF-8");
            //解析html
            Document doc = Jsoup.parse(content);
            try {
                // 主要是用于进行过滤掉参文房子参文doi混入文章doi
                doc.select("ReferenceList").remove();
            } catch (Exception e) {

            }
            String html = doc.html();
            List<Element> divList = new ArrayList<>();
            Elements pubmedArticles = doc.select("PubmedArticle");
            Elements pubmedBookArticles = doc.select("PubmedBookArticle");
            for (Element e : pubmedArticles) {
                divList.add(e);
            }
            for (Element e : pubmedBookArticles) {
                divList.add(e);
            }
            //
            List<Pubmed> pubmeds = PubmedUtils2.parseHtml(divList);
            System.out.println(pubmeds);
//        for (Pubmed pubmed : pubmeds) {
//            insertData(pubmed, use);
//        }

        } else {

        }


    }

    @SneakyThrows
    public static void insertData(Pubmed pubmed, Db use) {

        use.insert(new Entity().setTableName(insetTable)
                .set("pmid", pubmed.getPmid())
                .set("type", pubmed.getType())
                .set("article_title", pubmed.getArticleTitle())
                .set("source", pubmed.getSource())
                .set("intro", pubmed.getIntro())
                .set("jour", pubmed.getJour())
                .set("volume", pubmed.getVolume())
                .set("issue", pubmed.getIssue())
                .set("pages", pubmed.getPages())
                .set("vip", pubmed.getVip())
                .set("pub_date", pubmed.getPubDate())
                .set("medline_date", pubmed.getMedlineDate())
                .set("pub_year", pubmed.getPubYear())
                .set("med_nlm_id", pubmed.getMedNlmId())
                .set("author", pubmed.getAuthor())
                .set("author_extend", pubmed.getAuthorExtend())
                .set("affs", pubmed.getAffs())
                .set("pmcid", pubmed.getPmcid())
                .set("doi", pubmed.getDoi())
                .set("lan", pubmed.getLan())
                .set("ab_text", pubmed.getAbText())
                .set("copyright", pubmed.getCopyright())
                .set("keyword", pubmed.getKeyword())
                .set("pts", pubmed.getPts())
                .set("meshs", pubmed.getMeshs())
                .set("mesh_heading", pubmed.getMeshHeading())
                .set("mesh_headingy", pubmed.getMeshHeadingy())
                .set("subs", pubmed.getSubs())
                .set("grants", pubmed.getGrants())
                .set("ass_date", pubmed.getAssDate())
                .set("full_text", pubmed.getFullText())
                .set("art_type", pubmed.getArtType())
        );

    }

}
